<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Http接口测试页</title>
    <script type="text/javascript" src="res/jquery.min.js"></script>
    <script type="text/javascript">
		//创建读卡控件
        var CertCtl = new IDCertCtl();

		var timer = 0;
		//身份证读卡控件创建
        function IDCertCtl() {
			//创建用于与服务交换数据的对象
            this.xhr = createXmlHttp();
            this.type = "CertCtl";
            this.height = 0;
            this.width = 0;
			//连接
            this.connect = CertCtl_connect;
			//断开
            this.disconnect = CertCtl_disconnect;
			//获取状态
            this.getStatus = CertCtl_getStatus;
			//读卡
            this.readCert = CertCtl_readCert;
            this.readCard = CertCtl_readCard;
			//读IC卡序列号
			this.readICCardSN = CertCtl_readICCardSN;
			//读身份证物理卡号
			this.readIDCardSN = CertCtl_readIDCardSN;
			//读安全模块接口
			this.getSAMID = CertCtl_getSAMID;
			//读M1卡扇区内容
			this.readM1Card = CertCtl_readM1Card;
			//写M1卡扇区内容
			this.writeM1Card = CertCtl_writeM1Card;
			//处理CPU卡APDU指令
			this.handleAPDUCMD = CertCtl_handleAPDUCMD;
			//设置项目识别码
			//this.setProgramCode = CertCtl_setProgramCode;
			//核验项目识别码
			this.checkProgramCode = CertCtl_checkProgramCode;
			this.Routon_Mute = CertCtl_RoutonMute;
			this.Routon_UnMute = CertCtl_RoutonUnMute;
			this.setRepeatRead = CertCtl_setRepeatRead;
			this.enableRepeatRead = CertCtl_enableRepeatRead;
			this.disableRepeatRead = CertCtl_disableRepeatRead;
			//循环读卡
			this.cycleRead = CertCtl_cycleRead;
        }

		//创建XMLHttpRequest 对象，用于在后台与服务器交换数据
        function createXmlHttp() {
            var xmlHttp = null;
            //根据window.XMLHttpRequest对象是否存在使用不同的创建方式
             
            if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();                  //FireFox、Opera等浏览器支持的创建方式
            }
						else if(window.ActiveXObject)
						{
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器支持的创建方式
            }
            
						//xmlHttp.withCredentials=false;
            return xmlHttp;
        }

		//连接方法
        function CertCtl_connect() {
            var result = "";
			var iParam1 = "1"; 
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/connect", false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
			//返回值readyState   0: 请求未初始化
			//				    1: 服务器连接已建立
			//				    2：请求已接收
			//				    3: 请求处理中
			//				    4: 请求已完成，且响应已就绪
			//返回值status      200: "OK"
			//					404: 未找到页面
			//当返回值readyState为4且status为200时,为查询成功
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
        }

		//断开方法
        function CertCtl_disconnect() {
            var result = "";
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/disconnect", false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
        }
		//获取状态方法
        function CertCtl_getStatus() {
            var result = "";
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/getStatus", false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
        }

		//执行读卡操作
        function CertCtl_readCard() {
            var result = "";
            try {
				//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
                CertCtl.xhr.open("POST", "http://localhost:18889/api/readCard", false); //readCard读卡，不生成正反面仿复印件
                

				//发送请求
                CertCtl.xhr.send();
                if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                    result = CertCtl.xhr.responseText;
                    CertCtl.xhr.readyState = 1;
                }
            } catch (e) {

            }
            return result;
        }

		//执行读卡操作
        function CertCtl_readCert() {
            var result = "";
            try {
								//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
								CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/readCert", false);	  //readCert读卡，生成正反面仿复印件	
                

				//发送请求
                CertCtl.xhr.send();
                if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                    result = CertCtl.xhr.responseText;
                    CertCtl.xhr.readyState = 1;
                }
            } catch (e) {

            }
            return result;
        }
		
		//执行循环读卡操作
		function CertCtl_cycleRead()
		{
			var result = "";
			try {
				//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
                CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/readCard", false); //readCard读卡，不生成正反面仿复印件
				//CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/readCert", false);	  //readCert读卡，生成正反面仿复印件	
				//发送请求
                CertCtl.xhr.send();
                if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                    result = CertCtl.xhr.responseText;
                    CertCtl.xhr.readyState = 1;
                }
            } catch (e) {

            }
            return result;
		}
		
		//获取IC卡序列号
		function CertCtl_readICCardSN() {
			var result = "";
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/readICCardSN", false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
		}
		
		//获取身份证物理卡号
		function CertCtl_readIDCardSN() {
			var result = "";
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/readIDCardSN", false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
		}
		
		//获取SAMID
		function CertCtl_getSAMID() {
			var result = "";
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
			//getSAMID 获取的SAMID，格式为：05032013102800015247360103645891
			//getSAMIDStr 获取SAMID，格式为：05.03-20131028-0001524736-0103645891
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/getSAMIDStr", false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
		}
		
		//读M1卡扇区内容
		function CertCtl_readM1Card() {
			var result = "";
			var sid = "1";
			var bid = "1";
			var keyType = "1";
			var key = "ffffffffffff";
			if(document.getElementById("input_sid").value != "")
			{
				sid = document.getElementById("input_sid").value;
			}
			
			if(document.getElementById("input_bid").value != "")
			{
				bid = document.getElementById("input_bid").value;
			}
			
			if(document.getElementById("input_keytype").value != "")
			{
				keyType = document.getElementById("input_keytype").value;
			}
			
			if(document.getElementById("input_key").value != "")
			{
				key = document.getElementById("input_key").value;
			}
			
			
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
			CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/readM1Card?"+"SID="+sid+"&BID="+bid+"&KEYTYPE="+keyType+"&KEY="+key, false);
			
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
		}
		
		//写M1卡扇区内容
		function CertCtl_writeM1Card() {
			var result = "";
			var sid = "1";
			var bid = "1";
			var keyType = "1";
			var key = "ffffffffffff";
			var data = "00000000000000000000000000000000";
			if(document.getElementById("input_sid").value != "")
			{
				sid = document.getElementById("input_sid").value;
			}
			
			if(document.getElementById("input_bid").value != "")
			{
				bid = document.getElementById("input_bid").value;
			}
			
			if(document.getElementById("input_keytype").value != "")
			{
				keyType = document.getElementById("input_keytype").value;
			}
			
			if(document.getElementById("input_key").value != "")
			{
				key = document.getElementById("input_key").value;
			}
			
			if(document.getElementById("cmd").value != "")
			{
				data = document.getElementById("cmd").value;
			}
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
			CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/writeM1Card?"+"SID="+sid+"&BID="+bid+"&KEYTYPE="+keyType+"&KEY="+key+"&DATA="+data, false);
			//CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/writeM1Card?"+"SID="+sid+"&BID="+bid+"&KEYTYPE="+keyType+"&KEY="+key+"&DATA="+data, false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
		}
		
		//处理CPU卡APDU指令
		function CertCtl_handleAPDUCMD(){
			//var cmd = "00A4000000"; //基本命令（测试用）：选择MF下的根目录，返回值为：6F15840E315041592E5359532E4444463031A5038801019000
			var cmd = "";
			if(document.getElementById("cmd").value != "")
			{
				cmd = document.getElementById("cmd").value;
			}
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/handleAPDUCMD?"+"APDUCMD="+cmd, false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
		}
		
		/*
		//设置项目识别码
		function CertCtl_setProgramCode(){
			var programData = "";
			if(document.getElementById("programData").value != "")
			{
				programData = document.getElementById("programData").value;
				
			}
			
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/setProgramCode?"+"PROGRAMCODE="+programData, false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
		}
		*/
		//核验项目识别码
		function CertCtl_checkProgramCode(){
			var programData = "";
			if(document.getElementById("programData").value != "")
			{
				programData = document.getElementById("programData").value;
			}
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/checkProgramCode?"+"PROGRAMCODE="+programData, false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
		}
		
		//关闭声音
		function CertCtl_RoutonMute(){
			var result = "";
			var IsMute = true; //false - 开声音（默认开）， true - 关声音
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/Routon_Mute?"+"ISMUTE="+IsMute, false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
		}

		//开启声音
		function CertCtl_RoutonUnMute(){
			var result = "";
			var IsMute = false; //false - 开声音（默认开）， true - 关声音
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/Routon_Mute?"+"ISMUTE="+IsMute, false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
		}
		
		//设置重复读卡
		function CertCtl_setRepeatRead(){
			var result = "";
			var IsRepeat = false; //false - 不重复（默认开）， true - 重复
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/setRepeatRead?"+"ISREPEAT="+IsRepeat, false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
		}
		
		// 允许重复读卡
		function CertCtl_enableRepeatRead() {
			var result = "";
			var IsRepeat = true; //false - 不重复（默认开）， true - 重复
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/setRepeatRead?"+"ISREPEAT="+IsRepeat, false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
		}

		// 禁止重复读卡
		function CertCtl_disableRepeatRead() {
			var result = "";
			var IsRepeat = false; //false - 不重复（默认开）， true - 重复
			//创建请求 第一个参数是代表以post方式发送；第二个是请求端口和地址；第三个表示是否异步
            CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/setRepeatRead?"+"ISREPEAT="+IsRepeat, false);
			//发送请求
            try {
            	CertCtl.xhr.send();
            } catch (e) {
            }
            if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
                result = CertCtl.xhr.responseText;
                CertCtl.xhr.readyState = 1;
            }
            return result;
		}
				
		//转为Json格式
        function toJson(str) {
            //var obj = JSON.parse(str);
            //return obj;
            return eval('(' + str + ')');
        }

		//清空页面上显示的内容
        function clearForm() {
			//对应控件的值全部清空
            document.getElementById("timeElapsed").value = "";
            document.getElementById("certType").value = "";
            document.getElementById("timeElapsed").value = "";
            document.getElementById("venderId").value = "";
            document.getElementById("signature").value = "";
            document.getElementById("partyName").value = "";
            document.getElementById("gender").value = "";
            document.getElementById("nation").value = "";
            document.getElementById("bornDay").value = "";
            document.getElementById("certAddress").value = "";
            document.getElementById("certNumber").value = "";
            document.getElementById("certOrg").value = "";
            document.getElementById("effDate").value = "";
            document.getElementById("expDate").value = "";
            document.getElementById("result").value = "";
            document.getElementById("HeadPic").src = "";
            document.getElementById("PhotoDisplay").src = "";
            document.getElementById("FrontPic").src = "";
            document.getElementById("BackPic").src = "";
			//document.getElementById("programData").value = "";
			//document.getElementById("input_sid").value = "";
			//document.getElementById("input_bid").value = "";
			//document.getElementById("input_keytype").value = "";
			//document.getElementById("input_key").value = "";
			//document.getElementById("data").value = "";
			//document.getElementById("ACardResult").value = "";
        }

		//连接方法
        function connect() {
			//清空页面
            clearForm();

            try {
				//调用对应的连接方法,并赋值给result
                var result = CertCtl.connect();
				//如果result为空,代表读卡插件未启动
                if (result == "") {
                    alert("未启动读卡插件!")
                } else {
					//result页面回显
                    document.getElementById("result").value = result;
                }
            } catch (e) {
            }
        }
		
		//断开连接方法
        function disconnect() {
			//清空页面
            clearForm();

            try {
				//调用对应的断开连接方法,并赋值给result
                var result = CertCtl.disconnect();
				//如果result为空,代表读卡插件未启动
                if (result == "") {
                    alert("未启动读卡插件!")
                } else {
					//result页面回显
                    document.getElementById("result").value = result;
                }
            } catch (e) {
            }
        }

		//获取状态方法
        function getStatus() {
			//清空页面
            clearForm();

            try {
				//调用对应的获取状态方法,并赋值给result
                var result = CertCtl.getStatus();
				//如果result为空,代表读卡插件未启动
                if (result == "") {
                    alert("未启动读卡插件!")
                } else {
					//result页面回显
                    document.getElementById("result").value = result;
                }
            } catch (e) {
            }
        }
        
        //读卡方法
        function readCard() {
			//清空页面
            clearForm();
			//开始时间
            var startDt = new Date();
			//调用对应的读卡方法
            var result = CertCtl.readCard();
			//如果result为空,代表读卡插件未启动
            if (result == "") {
                alert("未启动读卡插件!")
            } else {
				//结束时间
                var endDt = new Date();
				//读卡时间回显
                document.getElementById("timeElapsed").value = (endDt.getTime() - startDt.getTime()) + "毫秒";
                document.getElementById("result").value = result;
                //var resultObj = toJson(result);
                //result = result.replace("\"resultFlag\":","\"resultFlag\":\"true\"");
				//格式化result
                //var resultObj = $.parseJSON(result);//windows10上面无法解析
				var resultObj = eval('(' +result+')');
				//resultFlag为0代表读卡成功
                if (resultObj.resultFlag == "0") {
					//回显相关数据
                    document.getElementById("signature").value = resultObj.signature;
                    document.getElementById("venderId").value = resultObj.venderId;
                    document.getElementById("certType").value = resultObj.resultContent.certType;
                    document.getElementById("partyName").value = resultObj.resultContent.partyName;
                    document.getElementById("gender").value = resultObj.resultContent.gender;
                    document.getElementById("nation").value = resultObj.resultContent.nation;
                    document.getElementById("bornDay").value = resultObj.resultContent.bornDay;
                    document.getElementById("certAddress").value = resultObj.resultContent.certAddress;
                    document.getElementById("certNumber").value = resultObj.resultContent.certNumber;
                    document.getElementById("certOrg").value = resultObj.resultContent.certOrg;
                    document.getElementById("effDate").value = resultObj.resultContent.effDate;
                    document.getElementById("expDate").value = resultObj.resultContent.expDate;
                    document.getElementById("HeadPic").src = "data:image/jpeg;base64," + resultObj.resultContent.identityPic;
				} else if (resultObj.resultFlag == "-1") {
                    if (resultObj.errorMsg == "端口打开失败") {
                        alert("读卡器未连接");
                    } else {
                        alert(resultObj.errorMsg);
                    }
                } else if (resultObj.resultFlag == "-2") {
                    alert(resultObj.errorMsg);
                }
            }
        }

		//读卡方法
        function readCert() {
			//清空页面
            clearForm();
			//开始时间
            var startDt = new Date();
			//调用对应的读卡方法
            var result = CertCtl.readCert();
			//如果result为空,代表读卡插件未启动
            if (result == "") {
                alert("未启动读卡插件!")
            } else {
				//结束时间
                var endDt = new Date();
				//读卡时间回显
                document.getElementById("timeElapsed").value = (endDt.getTime() - startDt.getTime()) + "毫秒";
                document.getElementById("result").value = result;
                //var resultObj = toJson(result);
                //result = result.replace("\"resultFlag\":","\"resultFlag\":\"true\"");
				//格式化result
                //var resultObj = $.parseJSON(result);//windows10上面无法解析
				var resultObj = eval('(' +result+')');
				//resultFlag为0代表读卡成功
                if (resultObj.resultFlag == "0") {
					//回显相关数据
                    document.getElementById("signature").value = resultObj.signature;
                    document.getElementById("venderId").value = resultObj.venderId;
                    document.getElementById("certType").value = resultObj.resultContent.certType;
                    document.getElementById("partyName").value = resultObj.resultContent.partyName;
                    document.getElementById("gender").value = resultObj.resultContent.gender;
                    document.getElementById("nation").value = resultObj.resultContent.nation;
                    document.getElementById("bornDay").value = resultObj.resultContent.bornDay;
                    document.getElementById("certAddress").value = resultObj.resultContent.certAddress;
                    document.getElementById("certNumber").value = resultObj.resultContent.certNumber;
                    document.getElementById("certOrg").value = resultObj.resultContent.certOrg;
                    document.getElementById("effDate").value = resultObj.resultContent.effDate;
                    document.getElementById("expDate").value = resultObj.resultContent.expDate;
                    document.getElementById("HeadPic").src = "data:image/jpeg;base64," + resultObj.resultContent.identityPic;
                    document.getElementById("PhotoDisplay").src = "data:image/jpeg;base64," + resultObj.resultContent.identityPrintPic;
                    document.getElementById("FrontPic").src = "data:image/jpeg;base64," + resultObj.resultContent.identityFrontPic;
                    document.getElementById("BackPic").src = "data:image/jpeg;base64," + resultObj.resultContent.identityBackPic;
				} else if (resultObj.resultFlag == "-1") {
                    if (resultObj.errorMsg == "端口打开失败") {
                        alert("读卡器未连接");
                    } else {
                        alert(resultObj.errorMsg);
                    }
                } else if (resultObj.resultFlag == "-2") {
                    alert(resultObj.errorMsg);
                }
            }
        }
		
		function readICCardSN() {
			//清空页面
            clearForm();

            try {
				//调用对应的获取状态方法,并赋值给result
				//CertCtl.Routon_Mute();
                var result = CertCtl.readICCardSN();
				//如果result为空,代表读卡插件未启动
                if (result == "") {
                    alert("未启动读卡插件!")
                } else {
					//result页面回显
                    document.getElementById("result").value = result;
                }
            } catch (e) {
            }
		}
		
		function readIDCardSN() {
			//清空页面
            clearForm();

            try {
				//调用对应的获取状态方法,并赋值给result
                var result = CertCtl.readIDCardSN();
				//如果result为空,代表读卡插件未启动
                if (result == "") {
                    alert("未启动读卡插件!")
                } else {
					//result页面回显
                    document.getElementById("result").value = result;
                }
            } catch (e) {
            }
		}
		
		function getSAMID() {
			//清空页面
            clearForm();

            try {
				//调用对应的获取状态方法,并赋值给result
                var result = CertCtl.getSAMID();
				//如果result为空,代表读卡插件未启动
                if (result == "") {
                    alert("未启动读卡插件!")
                } else {
					//result页面回显
                    document.getElementById("result").value = result;
                }
            } catch (e) {
            }
		}
		
		function readM1Card() {
			//清空页面
            clearForm();

            try {
				//调用对应的获取状态方法,并赋值给result
                var result = CertCtl.readM1Card();
				//如果result为空,代表读卡插件未启动
                if (result == "") {
                    alert("未启动读卡插件!")
                } else {
					//result页面回显
                    document.getElementById("ACardResult").value = result;
                }
            } catch (e) {
            }
		}
		
		function writeM1Card() {
			//清空页面
            clearForm();

            try {
				//调用对应的获取状态方法,并赋值给result
                var result = CertCtl.writeM1Card();
				//如果result为空,代表读卡插件未启动
                if (result == "") {
                    alert("未启动读卡插件!")
                } else {
					//result页面回显
                    document.getElementById("ACardResult").value = result;
                }
            } catch (e) {
            }
		}
		
		function handleAPDUCMD() {
			//清空页面
            clearForm();

            try {
				//调用对应的获取状态方法,并赋值给result
                var result = CertCtl.handleAPDUCMD();
				//如果result为空,代表读卡插件未启动
                if (result == "") {
                    alert("未启动读卡插件!")
                } else {
					//result页面回显
                    document.getElementById("ACardResult").value = result;
                }
            } catch (e) {
            }
		}
		
		/*
		function setProgramCode() {
			//清空页面
            clearForm();

            try {
				//调用对应的获取状态方法,并赋值给result
                var result = CertCtl.setProgramCode();
				//如果result为空,代表读卡插件未启动
                if (result == "") {
                    alert("未启动读卡插件!")
                } else {
					//result页面回显
                    document.getElementById("ACardResult").value = result;
                }
            } catch (e) {
            }
		}
		*/
		
		/* 检测输入内容
		function check()
		{
			var programCode = document.getElementById("programData").value;
			if(programCode == "")
			{
				alert("项目码不能为空");
				return false;
			}
			else if(programCode.length != 8)
			{
				alert("项目码为8个字符");
				return false;
			}
			else 
			{
				for(var i=0; i<programCode.length; i++)
				{
					if(!(programCode.charCodeAt(i)>='0'.charCodeAt() && programCode.charCodeAt(i)<='9'.charCodeAt())
					&& !(programCode.charCodeAt(i)>='a'.charCodeAt() && programCode.charCodeAt(i)<='z'.charCodeAt())
					&& !(programCode.charCodeAt(i)>='A'.charCodeAt() && programCode.charCodeAt(i)<='Z'.charCodeAt())
					)
					{
						alert("项目码只能包含英文字母和数字");
						return false;
					}
				}
			}
			return true;
		}
		*/
		
		function checkProgramCode() {
			//清空页面
            clearForm();

            try {
				//调用对应的获取状态方法,并赋值给result
                var result = CertCtl.checkProgramCode();
				//如果result为空,代表读卡插件未启动
                if (result == "") {
                    alert("未启动读卡插件!")
                } else {
					//result页面回显
                    document.getElementById("ACardResult").value = result;
                }
            } catch (e) {
            }
		}
		
		//定时器函数
		function testRead(){
			//开始时间
            var startDt = new Date();
			//调用对应的读卡方法
            var result = CertCtl.readCard();
			//如果result为空,代表读卡插件未启动
            if (result == "") {
                alert("未启动读卡插件!")
            } else {
				//结束时间
                var endDt = new Date();
				//读卡时间回显
                document.getElementById("timeElapsed").value = (endDt.getTime() - startDt.getTime()) + "毫秒";
                document.getElementById("result").value = result;
                //var resultObj = toJson(result);
                //result = result.replace("\"resultFlag\":","\"resultFlag\":\"true\"");
				//格式化result
                //var resultObj = $.parseJSON(result);//windows10上面无法解析
				var resultObj = eval('(' +result+')');
				//resultFlag为0代表读卡成功
                if (resultObj.resultFlag == "0") {
					//清空页面
					//clearForm();
					//回显相关数据
                    document.getElementById("signature").value = resultObj.signature;
                    document.getElementById("venderId").value = resultObj.venderId;
                    document.getElementById("certType").value = resultObj.resultContent.certType;
                    document.getElementById("partyName").value = resultObj.resultContent.partyName;
                    document.getElementById("gender").value = resultObj.resultContent.gender;
                    document.getElementById("nation").value = resultObj.resultContent.nation;
                    document.getElementById("bornDay").value = resultObj.resultContent.bornDay;
                    document.getElementById("certAddress").value = resultObj.resultContent.certAddress;
                    document.getElementById("certNumber").value = resultObj.resultContent.certNumber;
                    document.getElementById("certOrg").value = resultObj.resultContent.certOrg;
                    document.getElementById("effDate").value = resultObj.resultContent.effDate;
                    document.getElementById("expDate").value = resultObj.resultContent.expDate;
                    document.getElementById("HeadPic").src = "data:image/jpeg;base64," + resultObj.resultContent.identityPic;
                    document.getElementById("PhotoDisplay").src = "data:image/jpeg;base64," + resultObj.resultContent.identityPrintPic;	
					
				} else if (resultObj.resultFlag == "-1") {
                    if (resultObj.errorMsg == "端口打开失败") {
                        alert("读卡器未连接");
                    } else {
                        //alert(resultObj.errorMsg);
						//document.getElementById("result").value = resultObj.errorMsg;
                    }
                } else if (resultObj.resultFlag == "-2") {
                    //alert(resultObj.errorMsg);
					//document.getElementById("result").value = resultObj.errorMsg;
                }
            }
			
		}
		function testCycleRead(){
//			CertCtl.setRepeatRead();
			
			timer = setInterval(testRead, 5000);
		}
		
		function closeCycleRead(){
			clearInterval(timer);
		}
		
		function enableRepeatRead() {
			//清空页面
      clearForm();

      try {
				//调用对应的获取状态方法,并赋值给result
        var result = CertCtl.enableRepeatRead();
				//如果result为空,代表读卡插件未启动
        if (result == "") {
          alert("未启动读卡插件!")
        } else {
				//result页面回显
          document.getElementById("result").value = result;
        }
      } catch (e) {
      }
		}
		function disableRepeatRead() {
			//清空页面
      clearForm();

      try {
				//调用对应的获取状态方法,并赋值给result
        var result = CertCtl.disableRepeatRead();
				//如果result为空,代表读卡插件未启动
        if (result == "") {
          alert("未启动读卡插件!")
        } else {
				//result页面回显
          document.getElementById("result").value = result;
        }
      } catch (e) {
      }			
		}
		
		function Routon_Mute() {
			//清空页面
      clearForm();

      try {
				//调用对应的获取状态方法,并赋值给result
        var result = CertCtl.Routon_Mute();
				//如果result为空,代表读卡插件未启动
        if (result == "") {
          alert("未启动读卡插件!")
        } else {
				//result页面回显
          document.getElementById("result").value = result;
        }
      } catch (e) {
      }			
		}
		function Routon_UnMute() {
			//清空页面
      clearForm();

      try {
				//调用对应的获取状态方法,并赋值给result
        var result = CertCtl.Routon_UnMute();
				//如果result为空,代表读卡插件未启动
        if (result == "") {
          alert("未启动读卡插件!")
        } else {
				//result页面回显
          document.getElementById("result").value = result;
        }
      } catch (e) {
      }
		}		
    </script>
</head>
<body bgcolor="#D2F0FF">

<h1 style="text-align:center;color:#5555FF;">身份证阅读器USB接入测试页面</h1>

<table border="0" width="50%" cellpadding="0" cellspacing="0" style="padding-left:100px;">
    <tr>
        <td><input type="button" value="连接" onclick="connect()"></td>
        <td><input type="button" value="状态" onclick="getStatus()"></td>
        <td><input type="button" value="读卡(不合成照片)" onclick="readCard()"></td>
        <td><input type="button" value="读卡(合成照片)" onclick="readCert()"></td>
        <td><input type="button" value="断开" onclick="disconnect()"></td>
		<td><input type="button" value="读IC卡序列号" onclick="readICCardSN()"></td>
		<td><input type="button" value="读身份证物理卡号" onclick="readIDCardSN()"></td>
		<td><input type="button" value="读安全模块号" onclick="getSAMID()"></td>
		<td><input type="button" value="测试循环读卡" onclick="testCycleRead()"></td>
		<td><input type="button" value="关闭循环读卡" onclick="closeCycleRead()"></td>
		<td><input type="button" value="允许重复读卡" onclick="enableRepeatRead()"></td>
		<td><input type="button" value="禁止重复读卡" onclick="disableRepeatRead()"></td>
		<td><input type="button" value="关闭读卡提示音" onclick="Routon_Mute()"></td>
		<td><input type="button" value="开启读卡提示音" onclick="Routon_UnMute()"></td>
	</tr>
</table>
<br/>
<table border="0" width="100%" cellpadding="0" cellspacing="10">
    <tr>
        <td align="right">读卡时间：</td>
        <td><input type="text" id="timeElapsed" size="49" style="width:400px;" readonly="readonly"></td>
    </tr>
    <tr>
        <td align="right">数字签名：</td>
        <td><input type="text" id="signature" size="49" style="width:400px;" readonly="readonly"></td>
    </tr>
    <tr>
        <td align="right">厂家标识：</td>
        <td><input type="text" id="venderId" size="49" style="width:400px;" readonly="readonly"></td>
    </tr>
    <tr>
	    <td align="right">卡类型：</td>
  	  <td><input type="text" id="certType" size="49" style="width:400px;" readonly="readonly">(1-中国居民身份证，50-外国人永久居住证证，54-台湾居住证，55-港澳居住证)</td>
    </tr>
    <tr>
        <td align="right">中/英姓名：</td>
        <td><input type="text" id="partyName" size="49" style="width:400px;" readonly="readonly">(要求中间，两头都没有空格)</td>
    </tr>
    <tr>
        <td align="right">性别：</td>
        <td><input type="text" id="gender" size="49" style="width:400px;" readonly="readonly">(取值为“1”（表示“男”）或“0”（表示“女”）)
        </td>
    </tr>
    <tr>
        <td align="right">民族/国籍：</td>
        <td><input type="text" id="nation" size="49" style="width:400px;" readonly="readonly">(汉字即可)</td>
    </tr>
    <tr>
        <td align="right">出生日期：</td>
        <td><input type="text" id="bornDay" size="49" style="width:400px;" readonly="readonly">(要求格式为:yyyyMMdd，长度为8)
        </td>
    </tr>
    <tr>
        <td align="right">住址：</td>
        <td><input type="text" id="certAddress" size="49" style="width:400px;" readonly="readonly">(外国人永久居留身份证地址为“空”)
        </td>
    </tr>
    <tr>
        <td align="right">身份证号：</td>
        <td><input type="text" id="certNumber" size="49" style="color:#FF0000;width:400px;" readonly="readonly">(居民身份号码，长度18位)
        </td>
    </tr>
    <tr>
        <td align="right">签发机关：</td>
        <td><input type="text" id="certOrg" size="49" style="width:400px;" readonly="readonly">(外国永久居留身份证签发机关为“机关代码”)
        </td>
    </tr>
    <tr>
        <td align="right">开始期限：</td>
        <td><input type="text" id="effDate" size="49" style="width:400px;" readonly="readonly">(要求格式为:yyyyMMdd，长度为8)
        </td>
    </tr>
    <tr>
        <td align="right">结束期限：</td>
        <td><input type="text" id="expDate" size="49" style="width:400px;" readonly="readonly">(要求格式为:yyyyMMdd，长度为8，或者是“长期”)
        </td>
    </tr>
    <tr>
        <td align="right">结果：</td>
        <td><textarea id="result" rows="8" cols="47" style="width:400px;" readonly="readonly"></textarea></td>
    </tr>
    <tr>
        <td align="right">照片：</td>
        <td><img id="HeadPic" style="height: 126px; width: 102px" align="top" />
    </tr>
	<tr>
		<td>预览：</td>
		<td>
            <img id="PhotoDisplay" style="width:341px; height:443px;"/>
        </td>
		<td>正面：</td>
		<td>
            <img id="FrontPic" style="width:341px; height:222px;"/>
        </td>
		<td>背面：</td>
		<td>
            <img id="BackPic" style="width:341px; height:222px;"/>
        </td>
	</tr>
</table>
<br>
<br>
<br>
<br>
<table border="0" width="100%" cellpadding="0" cellspacing="10">
	 <tr>
        <!--td align="right"">扇区号：</td>
        <td><input type="text" id="sid" size="49" style="width:50px;"></td>
		<td align="right">块号：</td>
        <td><input type="text" id="bid" size="49" style="width:50px;"></td>
		<td align="right">密钥类型：</td>
        <td><input type="text" id="keyType" size="49" style="width:50px;"></td>
		<td align="right">密钥：</td>
        <td><input type="text" id="key" size="49" style="width:100px;"></td-->
		<td>
			扇区号（SID）：<input type="text" style="width:60px;hidth:30px" id="input_sid" onkeyup="value=value.replace(/\D/g,'')" oncontextmenu="return false;" onblur="valid('input_sid',15)">
			块号（BID）：<input type="text" style="width:60px;hidth:30px" id="input_bid" onkeyup="value=value.replace(/\D/g,'')" oncontextmenu="return false;" onblur="valid('input_bid',3)">
			秘钥类型（KeyType 1-keyA&nbsp;2-keyB）：<input type="text" id="input_keytype" onkeyup="value=value.replace(/\D/g,'')" oncontextmenu="return false;" onblur="valid('input_keytype',2)">
			秘钥（Key）：<input type="text" id="input_key">
		</td>
    </tr>
	<tr>
		<td >指令:<input type="text" id="cmd" size="49" style="width:400px;"></td>
	</tr>
</table>
<table border="0" width="100%" cellpadding="0" cellspacing="10">
	<tr>
        <!--td><input type="button" value="读M1卡" onclick="readM1Card()"></td>
        <td><input type="button" value="写M1卡" onclick="writeM1Card()"></td>
        <td><input type="button" value="发送APDU指令" onclick="handleAPDUCMD()"></td-->
		<td>
			<input type="button" value="读M1卡"  onclick="readM1Card()">
			<input type="button" value="写M1卡"  onclick="writeM1Card()">
			<input type="button" value="发送APDU指令" onclick="handleAPDUCMD()">
		</td>
	</tr>
	<tr>
	</tr>
	<tr>
		<td>项目码：<input type="text" id="programData" size="49" style="width:100px;"></td>
	</tr>
	<tr>
		<td>
			<!--input type="button" value="设置项目码" onclick="setProgramCode()"-->
			<input type="button" value="核验项目码" onclick="checkProgramCode()">
		</td>
	</tr>
	<tr>
        <td>结果：<textarea id="ACardResult" rows="8" cols="47" style="width:400px;" readonly="readonly"></textarea></td>
    </tr>
</table>
<object id="CertCtl" type="application/cert-reader" width="0" height="0">
    <object classid="clsid:30516390-004F-40B9-9FC6-C9096B59262E" id="CertCtl" width="0" height="0">
        <object ID="CertCtl" classid="30516390-004F-40B9-9FC6-C9096B59262E" TYPE="application/cert-reader" width=0
                height=0>
            <!--<p style="color:#FF0000;">控件不可用，可能未正确安装控件及驱动，或者控件未启用。</p>-->
        </object>
    </object>
</object>
</body>
</html>
